package com.example.figurehecheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.img.Img;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.figurehecheng.common.constant.MessageConstant;
import com.example.figurehecheng.common.context.BaseContext;
import com.example.figurehecheng.common.exception.AccountExist;
import com.example.figurehecheng.controller.merchant.CommonController;
import com.example.figurehecheng.domain.dto.ImgDTO;
import com.example.figurehecheng.domain.dto.SaveImgDTO;
import com.example.figurehecheng.domain.entity.Coupon;
import com.example.figurehecheng.domain.entity.Image;
import com.example.figurehecheng.domain.entity.Merchant;
import com.example.figurehecheng.domain.entity.Tourtable;
import com.example.figurehecheng.domain.vo.CouponVO;
import com.example.figurehecheng.domain.vo.ImageVO;
import com.example.figurehecheng.mapper.ImageMapper;
import com.example.figurehecheng.mapper.MerchantMapper;
import com.example.figurehecheng.service.IImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.figurehecheng.service.IMerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@Service
@RequiredArgsConstructor
public class ImageServiceImpl extends ServiceImpl<ImageMapper, Image> implements IImageService {

    private final ImageMapper imageMapper;

    private final CommonController commonController;

    private final MerchantMapper merchantMapper;

    @Autowired
    private IMerchantService iMerchantService;
    @Override
    public List<ImageVO> selectimg() {
        QueryWrapper<Merchant> merchantQueryWrapper = new QueryWrapper<Merchant>()
                .eq("id", BaseContext.getCurrentId());
        Merchant merchant = iMerchantService.getOne(merchantQueryWrapper);
        Long currentId = BaseContext.getCurrentId();
        List<ImageVO> img = new ArrayList<>();
        List<Image> coupons = lambdaQuery().
                eq(Image::getShopId, currentId)
                .eq(Image::getType,merchant.getRole())
                .list();
        coupons.forEach(item->{img.add(BeanUtil.copyProperties(item,ImageVO.class));});
        return img;

    }

    /**
     * 删除图片
     * @param id
     */
    @Override
    public void deleteimg(Integer id) {
        QueryWrapper<Image> queryWrapper = new QueryWrapper<Image>().eq("id", id)
                .eq("shopId", BaseContext.getCurrentId());
        remove(queryWrapper);
    }

    @Override
    public String sebyid(Integer id) {
    Image image = Image.builder().build();
          image = lambdaQuery().
                eq(Image::getId, id)
                .one();
        String photograph = image.getPhotograph();
        return photograph;
    }

    @Override
    public void saveimg(String image) {
        QueryWrapper<Merchant> merchantQueryWrapper = new QueryWrapper<Merchant>()
                .eq("id", BaseContext.getCurrentId());
        Merchant merchant = iMerchantService.getOne(merchantQueryWrapper);
        Image images = Image.builder().photograph(image)
                .shopId(BaseContext.getCurrentId().intValue())
                .type(merchant.getRole().intValue())
                .build();
        save(images);
    }

    /**
     * 根据项目id和商家id查询图片
     * @param programId
     * @return
     */
    @Override
    public List<Image> selectHouseImg(Integer programId) {
        return imageMapper.selectList( new LambdaQueryWrapper<Image>().eq(Image::getProgramId, programId).eq(Image::getShopId, BaseContext.getCurrentId()));
    }

    /**
     * 新增房间图片
     * @param
     * @return
     */
    @Override
    public void saveHouseImg(SaveImgDTO saveImgDTO) {
        Image image = new Image();
        image.setPhotograph(saveImgDTO.getPhotograph());
        image.setProgramId(saveImgDTO.getProgramId());
        image.setShopId(Math.toIntExact(BaseContext.getCurrentId()));
        save(image);

    }


    /**
     * 删除房间图片
     * @param photograph
     * @return
     */
    @Override
    @Transactional
    public void deleteHouseImg(String photograph) {
        commonController.delete(photograph);
        remove(new LambdaQueryWrapper<Image>().eq(Image::getPhotograph, photograph));
    }

    /**
     * 查询我要游轮播图
     * @return
     */
    @Override
    public List selectTourImg() {
        //分页条件
        Page<Image> imagePage = new Page<>(0, 3);
        //获取我要游图片类型的商家id
        LambdaQueryWrapper<Merchant> eq = new LambdaQueryWrapper<Merchant>()
                .select(Merchant::getId)
                .eq(Merchant::getRole, Merchant.MY_Travel);
        List<Merchant> merchants = merchantMapper.selectList(eq);
        List<Integer> integers = new ArrayList<>();
        for (Merchant merchant : merchants) {
            integers.add(merchant.getId());
        }

        //构造查询条件
        LambdaQueryWrapper<Image> imageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询
        Page<Image> page = imageMapper.selectPage(imagePage, imageLambdaQueryWrapper);
        //返回数据
        List<Image> records = page.getRecords();
        List img = new ArrayList<>();
        for (Image record : records) {
            img.add(record.getPhotograph());
        }
        return img;
    }
}
